<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------

namespace Wechat\Controller;

use Think\Controller;

class BaseController extends Controller
{

    /**
     * BaseController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        //开启模板
        layout('Public_layout');

        header('Content-Type:text/html;charset=utf-8');
        if(I('get.state')=='test'){
            session('user.user_id',32088);
            session('user_id',32088);
        }
        if (!session('user.user_id')) {
            $code = I('get.code');
            if (empty($code)) {
                exit('<h1>请关注公众号“萧山新华书店小书虫俱乐部”来访问此页面</h1>');
            } else {
                //此处防止微信二次跳转导致code失效
                if(session('weixin_code')==$code){
                    return;
                }else{
                    session('weixin_code',$code);
                }

                $access_token = S('access_token');
                if (empty($access_token)) {
                    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . C('wechat.AppID') . '&secret=' . C('wechat.AppSecret');
                    $str = file_get_contents($url);
                    $str = json_decode($str, true);
                    $access_token = $str['access_token'];
                    S('access_token', $access_token, 3600);
                }

                $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . C('wechat.AppID') . '&secret=' . C('wechat.AppSecret') . "&code=$code&grant_type=authorization_code";
                $str = file_get_contents($url);
                $str = json_decode($str, true);
                $openid = $str['openid'];
                $r = M('user')->where(['openid' => $openid])->find();
                if (!empty($r['user_id'])) {
                    session('user.user_id', $r['user_id']);
                    session('user_id', $r['user_id']);
                } else {
                    $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
                    $str = file_get_contents($url);
                    $str = json_decode($str, true);
                    if (empty($str['errcode'])) {
                        $user_id = M('user')->add($str);
                        $str['user_id'] = $user_id;
                        if (!empty($r)) {
                            M('wechat')->where(['openid' => $r['openid']])->save($str);
                        } else {
                            M('wechat')->add($str);
                        }
                        $c=M('Card')->where(['user_id'=>$user_id])->find();
                        if($c<1){
                            M('Card')->add(['user_id'=>$user_id]);
                        }
                        session('user', $str);
                        session('user_id', $str['user_id']);
                    } else {
                        exit('系统繁忙，请稍后再试');
                    }
                }
            }
        }
    }
    /**
     * 空操作
     */
    public function _empty()
    {
        layout(false);
        $this->display('Public_404');
    }
}